package com.ushine.design.function;


import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @Author: yls
 * @Date 2022/1/26 16:44
 * @Description: 函数式编程
 * 用上了Java8的新特性lambda表达式
 * 判断条件放在key中
 * 对应的业务逻辑放在value中
 * @Version 1.0
 **/
public class QueryGrantTypeService {

    private GrantTypeService grantTypeService = new GrantTypeService();

    private Map<String, Function<String, String>> grantTypeMap = new HashMap<>(3);

    //    @PostConstruct
    public void dispatcherInt() {
        grantTypeMap.put("红包", resourceId -> grantTypeService.redPaper(resourceId));
        grantTypeMap.put("购物券", resourceId -> grantTypeService.shopping(resourceId));
        grantTypeMap.put("qq会员", resourceId -> grantTypeService.qqVip(resourceId));
    }

    public String getResult(String resourceType) {
        Function<String, String> function = grantTypeMap.get(resourceType);
        if (function != null) {
            // 传入参数：resourceId 执行表达式获得String类型的grantype
            return function.apply(resourceType);
        }
        return "查询不到该优惠券的发放方式！";
    }

    public static void main(String[] args) {
        QueryGrantTypeService queryGrantTypeService = new QueryGrantTypeService();
        queryGrantTypeService.dispatcherInt();
        String result = queryGrantTypeService.getResult("红包");
        System.out.println(result);

        result = queryGrantTypeService.getResult("购物券");
        System.out.println(result);

        result = queryGrantTypeService.getResult("qq会员");
        System.out.println(result);

    }
}
